#include "pixelhistory.h"

PixelHistory::PixelHistory(const PixelHistory& other)
{
	for (vector<Neighborhood2>::const_iterator it = other.memory.begin(); it != other.memory.end(); it++)
		this->memory.push_back(*it);
}

PixelHistory::PixelHistory (const EightLayerIM& images, int row, int col)
{
	for (int i = 0; i < images.getValidImages()-1; i++) // the last image is not  part of history
		this->memory.push_back(Neighborhood2(images.getImage(i), row, col, NRADIUS));
}

PixelHistory::PixelHistory (const EightLayerIM& images, int row, int col, int historySize)
{
	assert(historySize <= images.getValidImages() && "There are not enough images in the eight layer IM to make this history");
	for (int i = 0; i < historySize; i++) 
		this->memory.push_back(Neighborhood2(images.getImage(i), row, col, NRADIUS));
}

PixelHistory::PixelHistory (const EightLayerIM& images, int row, int col, int firstLayer, int lastLayer, int nbRadius)
{
	assert(firstLayer <= lastLayer && firstLayer >= 0 && lastLayer < images.getValidImages());
	for (int i = firstLayer; i <=lastLayer; i++) 
		this->memory.push_back(Neighborhood2(images.getImage(i), row, col, nbRadius));
}

void PixelHistory::fillFromELIMLayers (const EightLayerIM& elim, int firstLayer, int lastLayer, int row, int col, int nbRadius)
{
	assert(firstLayer <= lastLayer && "Corrupted layer indices.");
	if (memory.size() > 0)
		memory.clear();
	for (int i = firstLayer; i <= lastLayer; i++) 
		this->memory.push_back(Neighborhood2(elim.getImage(i), row, col, nbRadius));
}

PixelHistory& PixelHistory::operator= (const PixelHistory& other)
{
	if (&other == this)
		return *this;
	this->memory.clear();
	for (int i = 0; i < (int) other.memory.size(); i++)
		this->memory.push_back(other.memory[i]);
	return *this;
}

bool PixelHistory::operator== (const PixelHistory& other)
{
	if (this->memory.size() != other.memory.size())
		return false;
	for (int i = 0; i < (int) memory.size(); i++)
		if (!this->memory[i].hasSameFeature(other.memory[i]))
			return false;
	return true;
}

LongInstance PixelHistory::makeLongInstance (int label) const
{
	LongInstance li;
	for (int i = 0; i < (int) this->memory.size(); i++)
		li.addPhase(this->memory[i],label);
	return li;
}

int PixelHistory::getNBAtTime(int nbIndex, int timeStep) const
{
	return this->memory[timeStep].getFeature(nbIndex);
}


ostream& operator<< (ostream& stream, const PixelHistory& ph)
{
	stream << "PixelHistory:\n";
	//vector<Neighborhood2>::const_iterator it = ph.memory.begin();
	//for (; it != ph.memory.end(); ++it)
	for (int i = 0; i < (int) ph.memory.size(); ++i)
		stream << "History" << i << ": " << ph.memory[i] << endl;
	return stream;
}